package org.opens.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.opens.dao.LotteryHistoryDao;
import org.opens.entity.LotteryHistory;
import org.opens.entity.vo.LotteryHistoryModifyWave;
import org.opens.entity.vo.LotteryHistorySumDistribution;
import org.opens.factory.lottery.LotteryApi;
import org.opens.factory.lottery.LotteryApiFactory;
import org.opens.factory.lottery.other.LotteryTypes;
import org.opens.service.LotteryHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class LotteryHistoryServiceImpl implements LotteryHistoryService {

    @Autowired
    private LotteryHistoryDao lotteryHistoryDao;

    @Autowired
    private LotteryApiFactory lotteryApiFactory;

    @Override
    public int syncLocalDbFromRemote(String type) {
        if (StrUtil.isEmpty(type)) {
            type = LotteryTypes.SSQ;
        }
        List<LotteryHistory> theLatestArray = lotteryHistoryDao.getTheLatest(type, 1);
        LotteryApi lotteryApi = lotteryApiFactory.get();
        List<LotteryHistory> remoteDate = new ArrayList<>();
        PageResult<LotteryHistory> lotteryHistories = null;

        if (CollectionUtil.isEmpty(theLatestArray)) {
            lotteryHistories = lotteryApi.historyQuery(type, 50, 1);
            remoteDate.addAll(lotteryHistories);
            for (int i = 2; i <= lotteryHistories.getTotalPage(); i++) {
                lotteryHistories = lotteryApi.historyQuery(type, 50, i);
                remoteDate.addAll(lotteryHistories);
            }
        } else {
            LotteryHistory theLatest = theLatestArray.get(0);
            lotteryHistories = lotteryApi.historyQuery(type, 1, 1);
            for (int i = 1; i <= lotteryHistories.getTotalPage(); i++) {
                lotteryHistories = lotteryApi.historyQuery(type, 50, i);
                // 求出远程中的最小值
                String remoteMinOfPeriods = lotteryHistories.stream().map(LotteryHistory::getNumOfPeriods)
                        .filter(Objects::nonNull).min(Comparator.naturalOrder()).get();
                // 过滤掉库中已经存在的值
                List<LotteryHistory> collect = lotteryHistories.stream().filter(x -> {
                    return x.getNumOfPeriods().compareTo(theLatest.getNumOfPeriods()) > 0;
                }).collect(Collectors.toList());
                remoteDate.addAll(collect);
                // 跳出条件
                if (theLatest.getNumOfPeriods().compareTo(remoteMinOfPeriods) >= 0) {
                    break;
                }
            }
        }
        if (CollectionUtil.isEmpty(remoteDate)) {
            return 0;
        }
        return lotteryHistoryDao.saveBatch(remoteDate);
    }

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("s1", "s5", "s7", "s232");
        String s = strings.stream().max(Comparator.naturalOrder()).get();
        System.out.println(s);
    }

    @Override
    public IPage<LotteryHistory> page(int pageNumber, int pageSize, LotteryHistory searchParam) {
        Page<LotteryHistory> page = new Page<>(pageNumber, pageSize);
        return lotteryHistoryDao.page(page, searchParam);
    }

    @Override
    public List<LotteryHistory> getTheLatest(String type, Integer limit) {
        Assert.notEmpty(type, "类型不能为空");
        Assert.notNull(limit, "获取条数不能为空");

        if (limit > 365) {
            limit = 365;
        }

        return lotteryHistoryDao.getTheLatest(type, limit);
    }

    @Override
    public List<LotteryHistoryModifyWave> getModifyWave(String type, Integer limit) {
        Assert.notEmpty(type, "类型不能为空");
        Assert.notNull(limit, "获取条数不能为空");

        if (limit > 365) {
            limit = 365;
        }

        return lotteryHistoryDao.getModifyWave(type, limit);
    }

    @Override
    public List<LotteryHistorySumDistribution> getSSQLotteryHistorySumDistribution() {
        return lotteryHistoryDao.getSSQLotteryHistorySumDistribution();
    }
}
